import { _decorator, Animation, Color, Component, find, instantiate, Node, Prefab, ProgressBar, Sprite, SpriteFrame, UITransform, v3 } from 'cc';
import { getDistance, lookAtTarget, trackTarget, trackTargetMove } from './utils';
import { EnemyBullet } from './EnemyBullet';
import { GameMgr } from './GameMgr';
import { UserInfo } from './UserInfo';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {
    @property(Prefab)
    bulletPre: Prefab;
    @property(ProgressBar)
    hpBar:ProgressBar;
    @property(SpriteFrame)
    attackSpriteFrame:SpriteFrame;

    moveSpeed=70;

    tempShot=0
    playerNode=null;

    // isAlreadyArrive=false;
    maxHp;
    private hp;

    set Hp(value) {
        this.hp = value;
        if (this.hp <= 0) {
            this.die();
            return;
        }
        AudioMgr.ins.playOneShot("audio/hited");

        this.node.getChildByName("sp").getComponent(Sprite).color = new Color().fromHEX("#FF8B8B");

        this.scheduleOnce(() => {
            this.node.getChildByName("sp").getComponent(Sprite).color = Color.WHITE;
        }, 0.2)

        this.hpBar.progress = this.hp / this.maxHp;
    }
    get Hp() {
        return this.hp;
    }

    protected start(): void {
        this.playerNode = find("Canvas/playerRoot");
        this.maxHp = 5+UserInfo.gateLevel;
        this.hp = this.maxHp;

        this.node.getComponent(Animation).play("soldierRun");

      console.log(GameMgr.ins.enemyRoot.getComponent(UITransform).convertToNodeSpaceAR(this.playerNode.getWorldPosition()),"sss");


    }

    protected update(dt: number): void {
        this.tempShot += dt;
        if (this.tempShot > 2) {
            this.tempShot = 0;
            if (getDistance(this.node.position.x, 0, this.playerPos.x, 0) < 600) {
                this.createBullet();
            }
        }


        this.trackTargetMove(this.playerPos, this.node, this.moveSpeed, dt);

    }

    get playerPos(){
        return GameMgr.ins.enemyRoot.getComponent(UITransform).convertToNodeSpaceAR(this.playerNode.getWorldPosition());
    }

    createBullet() {
        AudioMgr.ins.playOneShot("audio/shot");

        let pre = instantiate(this.bulletPre);
        pre.setParent(find("Canvas/bulletRoot"));
        let pos=GameMgr.ins.bulletRoot.getComponent(UITransform).convertToNodeSpaceAR(this.node.getChildByName("shotPos").getWorldPosition());
        pre.setPosition(pos.x, pos.y, 0);
        pre.getComponent(EnemyBullet).init(this.playerNode.position);

    }

    tempPos = v3();
    /**
      * @description: 面向敌人移动
      * @return {*}
      */
    trackTargetMove(targetPos, selfNode, speed, dt) {
        if (!targetPos) {
            return
        }

        if (this.node.getWorldPosition().x > GameMgr.ins.player.getWorldPosition().x) {
            this.node.setScale(1, 1, 1);
        }
        else {
            this.node.setScale(-1, 1, 1);
        }

        if(!this.node.getComponent(Animation).getState("soldierRun").isPlaying){
            this.node.getComponent(Animation).play("soldierRun");
        }

        //如果自己已经到达敌人附近直接返回
        if (getDistance(targetPos.x, 0, selfNode.position.x, 0) < 400) {
            this.node.getComponent(Animation).stop();
            this.node.getChildByName("sp").getComponent(Sprite).spriteFrame = this.attackSpriteFrame;
            return
        }

        let speedArr = trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed * dt);

        selfNode.getPosition(this.tempPos);
        this.tempPos.x += speedArr[0]
        // this.tempPos.y += speedArr[1]
        selfNode.setPosition(this.tempPos);

        // selfNode.angle = speedArr[2] * (180 / Math.PI);


    }

    die(){
        GameMgr.ins.enemyExistDic.deleteValue(this.node);
        this.node.destroy();
    }
}


